package com.zmn.plat.admin.controller.orderconfig.distribute;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.DictModel;
import com.zmn.common.dictionary.KvModel;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.mcc.permit.client.annotation.FilterPermitData;
import com.zmn.plat.common.constant.DistributeWeightRelationConstant;
import com.zmn.plat.common.constant.OrderConfigConsts;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.common.dictionary.DistributeWayEnum;
import com.zmn.plat.dubbo.constant.DistributeConsts;
import com.zmn.plat.dubbo.constant.PlatConsts;
import com.zmn.plat.model.entity.company.BaseCompany;
import com.zmn.plat.model.entity.company.BaseCompanyQuery;
import com.zmn.plat.model.entity.orderconfig.distribute.*;
import com.zmn.plat.model.vo.orderconfig.distribute.DistributeWeightCompanyVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 类描述：派单参数配置页面跳转控制器
 *
 * @author mujunlin
 * @version v1.0
 * @since 2019/10/17 14:17
 */
@Slf4j
@RestController
@RequestMapping("/orderconfig/distribute/")
public class DistributeWeightListController extends DistributeWeightBaseController {

    /**
     * 派单参数平台配置页面跳转
     *
     * @return 平台配置页面
     */
    @RequestMapping(value = "listOfBizTypeC")
    @RequiresPermissions("/orderconfig/distribute/listOfBizTypeC.action")
    @ReadOnlyConnection
    public ModelAndView listOfBizTypeC() {
        ModelAndView mav = new ModelAndView("orderconfig/distribute/list");
        return list(mav, ProductConsts.BIZ_TYPE_C);
    }

    /**
     * 派单参数平台配置页面跳转
     *
     * @return 平台配置页面
     */
    @RequestMapping(value = "listOfBizTypeF")
    @RequiresPermissions("/orderconfig/distribute/listOfBizTypeF.action")
    @ReadOnlyConnection
    public ModelAndView listOfBizTypeF() {
        ModelAndView mav = new ModelAndView("orderconfig/distribute/biztypef/list");
        return list(mav, ProductConsts.BIZ_TYPE_F);
    }

    /**
     * 派单参数平台配置页面跳转
     *
     * @return 平台配置页面
     */
    @RequestMapping(value = "list")
    @RequiresPermissions("/orderconfig/distribute/list.action")
    @ReadOnlyConnection
    private ModelAndView list(ModelAndView mav, Integer bizType) {

        List<Integer> platList = listPermitedPlatByStaff(getMcStaff());

        List<DistributeWeightPlat> listAll = distributeWeightPlatService.listByPlatIdAndBizType(platList, bizType, null);
        List<DistributeWeightPlat> distributeWeightList = distributeWeightPlatService.listCompanyByPlatIdAndType(platList, bizType, null);
        List<DistributeWeightPlat> distributeWeightPlatDirectlyList = listAll.stream().filter(e -> DistributeConsts.DISTRIBUTE_PLAT_DIRECTLY.equals(e.getDistributeWay())).collect(Collectors.toList());
        List<Integer> distributeConfigIdList = listAll.stream().map(DistributeWeightPlat::getDistributePlatId).collect(Collectors.toList());
        List<Integer> configIdList = distributeWeightList.stream().map(DistributeWeightPlat::getDistributePlatId).collect(Collectors.toList());
        distributeConfigIdList.addAll(configIdList);
        listAll.addAll(distributeWeightList);
        List<Integer> robOrderConfigIdList = listAll.stream().map(DistributeWeightPlat::getConfigId).collect(Collectors.toList());
        DistributeWeightRelationQuery query = DistributeWeightRelationQuery.builder()
                .distributeConfigIdList(distributeConfigIdList)
                .distributeApplication(DistributeWeightRelationConstant.DISTRIBUTE_APPLICATION_PLAT)
                .build();
        List<DistributeWeightRelation> distributeWeightRelationList = CollectionUtil.isNullOrEmpty(distributeConfigIdList) ?
                new ArrayList<>() :
                distributeWeightRelationService.listByQuery(query);
        Map<Integer, List<DistributeWeightRelation>> distributeWeightRelationMap = distributeWeightRelationList.stream().collect(Collectors.groupingBy(DistributeWeightRelation::getDistributeConfigId));
        distributeWeightList.forEach(d -> d.setDistributeWeightRelationList(distributeWeightRelationMap.get(d.getDistributePlatId())));
        distributeWeightPlatDirectlyList.forEach(d -> d.setDistributeWeightRelationList(distributeWeightRelationMap.get(d.getDistributePlatId())));
        //获取抢单配置关联数据
        RobOrderRelationQuery robOrderRelationQuery = RobOrderRelationQuery.builder()
                .robOrderConfigIdList(robOrderConfigIdList)
                .build();
        List<RobOrderRelation> robOrderRelationList = CollectionUtil.isNullOrEmpty(robOrderConfigIdList) ?
                new ArrayList<>() :
                robOrderConfigBService.listByQuery(robOrderRelationQuery);
        Map<Integer, List<RobOrderRelation>> robOrderRelationMap = robOrderRelationList.stream().collect(Collectors.groupingBy(RobOrderRelation::getConfigId));
        distributeWeightList.forEach(d -> d.setRobOrderRelationList(robOrderRelationMap.get(d.getConfigId())));
        distributeWeightPlatDirectlyList.forEach(d -> d.setRobOrderRelationList(robOrderRelationMap.get(d.getConfigId())));
        mav.addObject("bizType", bizType);
        mav.addObject("items_plat", distributeWeightPlatDirectlyList);
        mav.addObject("size", distributeWeightPlatDirectlyList.size());
        mav.addObject("items_company", distributeWeightList);
        mav.addObject("platList", GlobalConsts.getPlatList());
        mav.addObject("autoStatusSet", EnumSet.allOf(OrderConfigConsts.AutoStatusEnum.class));
        mav.addObject("reworkStatusSet", EnumSet.allOf(OrderConfigConsts.ReworkStatusEnum.class));
        mav.addObject("normalStatusSet", EnumSet.allOf(OrderConfigConsts.StatusEnum.class));
        return mav;
    }

    /**
     * 派单参数平台配置新增修改页面跳转
     *
     * @param distributePlatId 平台ID
     * @param distributeWay    10 平台派单 20 服务商派单
     * @return 平台配置新增修改页面
     */
    @RequestMapping(value = "edit")
    @ReadOnlyConnection
    public ModelAndView edit(Integer configId,Integer distributePlatId, Integer distributeWay, Integer bizType) {

//        ModelAndView mav = Objects.equals(bizType, ProductConsts.BIZ_TYPE_C) ? new ModelAndView("orderconfig/distribute/edit")
//                : new ModelAndView("orderconfig/distribute/biztypef/edit");
        // 修改称用一个页面展示
        ModelAndView mav = new ModelAndView("orderconfig/distribute/edit");

        // 新增的时候 现在只有啄木鸟平台
        DistributeWeightPlat platConfig = NumberUtil.isNullOrZero(distributePlatId) ?
                DistributeWeightPlat.builder().bizType(bizType).distributeWay(distributeWay).plat(GlobalConsts.PLAT_MARK_ZMN).build()
                : distributeWeightPlatBService.findByKey(distributePlatId);
        DistributeWeightRelationQuery query = DistributeWeightRelationQuery.builder()
                .distributeConfigId(platConfig.getDistributePlatId())
                .distributeApplication(DistributeWeightRelationConstant.DISTRIBUTE_APPLICATION_PLAT)
                .build();
        List<DistributeWeightRelation> distributeWeightRelationList = NumberUtil.isNullOrZero(distributePlatId) ? new ArrayList<>() :
                distributeWeightRelationService.listByQuery(query);
        platConfig.setDistributeWeightRelationList(distributeWeightRelationDefaultData(distributeWeightRelationList, DistributeWeightRelationConstant.DISTRIBUTE_APPLICATION_PLAT, platConfig.getDistributePlatId()));
        //新增/修改抢单配置相关信息
        RobOrderConfigInfo robOrderConfigInfo = NumberUtil.isNullOrZero(configId)?
                RobOrderConfigInfo.builder().bizType(bizType).distributeWay(distributeWay).plat(GlobalConsts.PLAT_MARK_ZMN).build()
                :robOrderConfigBService.findByKey(configId);
        RobOrderRelationQuery robOrderRelationQuery = RobOrderRelationQuery.builder()
                .configId(robOrderConfigInfo.getConfigId())
                .build();
        List<RobOrderRelation> robOrderRelationList = NumberUtil.isNullOrZero(configId)
                ?Collections.emptyList()
                :robOrderConfigBService.listByQuery(robOrderRelationQuery);
        robOrderConfigInfo.setRobOrderRelationList(robOrderRelationList);
        //获取抢单配置关联产品数据

        if(CollectionUtil.isNotNullOrEmpty(robOrderConfigInfo.getRobOrderRelationList())){
            robOrderConfigInfo.getRobOrderRelationList().forEach(p->{
                RobOrderRelationProductQuery robOrderRelationProductQuery =RobOrderRelationProductQuery.builder()
                        .relationId(p.getRelationId())
                        .build();
                List<RobOrderRelationProduct> robOrderRelationProductList = robOrderConfigBService.listProductByQuery(robOrderRelationProductQuery);
                p.setRobOrderRelationProductList(robOrderRelationProductList);
            });
        }

        mav.addObject("obj", platConfig);
        mav.addObject("robOrderObj", robOrderConfigInfo);
        mav.addObject("engineerList", DistributeWayEnum.getEngineerList());
        mav.addObject("platList", GlobalConsts.getPlatList());
        StringBuilder editPermission = new StringBuilder();
        editPermission.append("/orderconfig/distribute/plat/");
        editPermission.append(Objects.equals(platConfig.getDistributeWay(), DistributeWayEnum.DISTRIBUTE_WAY_PLAT.getCode()) ? "direct/" : "");
        editPermission.append(platConfig.getBizType() == ProductConsts.WARRANTY_TYPE_OUT ? "modify.action" : "modifyf.action");
        mav.addObject("permission", editPermission.toString());
        mav.addObject("provinceList", distributeWeightPlatBService.listProvince(platConfig.getProvinceId()));
        boolean provinceDisable = platConfig != null && !NumberUtil.isNullOrZero(platConfig.getProvinceId());
        mav.addObject("provinceDisable", provinceDisable);
        SearchCityQuery searchCityQuery = new SearchCityQuery();
        BeanUtils.copyProperties(platConfig, searchCityQuery);
        searchCityQuery.setSelectedValue(platConfig.getCityId());
        mav.addObject("cityList", distributeWeightPlatBService.listCityByProvince(searchCityQuery));
        boolean cityDisable = platConfig != null && !NumberUtil.isNullOrZero(platConfig.getCityId());
        mav.addObject("cityDisable", cityDisable);

        return mav;
    }

    private List<DistributeWeightRelation> distributeWeightRelationDefaultData(List<DistributeWeightRelation> distributeWeightRelationList,
                                                                               Integer distributeApplication,
                                                                               Integer distributeConfigId) {

        assert distributeWeightRelationList != null;
        // 直营工程师权重
        DistributeWeightRelation distributeWeightRelation = DistributeWeightRelation
                .builder()
                .weightType(DistributeWeightRelationConstant.WEIGHT_TYPE_DIRECTLY)
                .distributeApplication(distributeApplication)
                .distributeConfigId(distributeConfigId)
                .build();
        DistributeWeightRelation distributeWeightRelationNon = DistributeWeightRelation
                .builder()
                .weightType(DistributeWeightRelationConstant.WEIGHT_TYPE_DIRECTLY_NON)
                .distributeApplication(distributeApplication)
                .distributeConfigId(distributeConfigId)
                .build();
        if (distributeWeightRelationList.size() == 0) {
            distributeWeightRelationList.add(distributeWeightRelation);
            distributeWeightRelationList.add(distributeWeightRelationNon);
        } else if (distributeWeightRelationList.size() == 1) {
            DistributeWeightRelation d = distributeWeightRelationList.get(0);
            if (DistributeWeightRelationConstant.WEIGHT_TYPE_DIRECTLY.equals(d.getWeightType())) {
                distributeWeightRelationList.add(distributeWeightRelationNon);
            } else if (DistributeWeightRelationConstant.WEIGHT_TYPE_DIRECTLY_NON.equals(d.getWeightType())) {
                distributeWeightRelationList.add(distributeWeightRelation);
            } else {
                distributeWeightRelationList.add(distributeWeightRelation);
                distributeWeightRelationList.add(distributeWeightRelationNon);
            }
        }
        return distributeWeightRelationList.stream().sorted(Comparator.comparingInt(DistributeWeightRelation::getWeightType)).collect(Collectors.toList());
    }

    /**
     * 派单参数公司配置页面跳转
     *
     * @param query 查询条件
     * @return 公司配置页
     */
    @RequestMapping(value = "company/list")
    @ReadOnlyConnection
    @FilterPermitData
    public ModelAndView companyList(DistributeWeightCompanyQuery query) {
        ModelAndView mav = Objects.equals(query.getBizType(), ProductConsts.BIZ_TYPE_C)
                ? new ModelAndView("orderconfig/distribute/company/list") : new ModelAndView("orderconfig/distribute/biztypef/company/list");
        log.info("#派单参数配置#服务商配置#列表查询 ： query = {}", query);
        List<DistributeWeightCompany> distributeWeightCompanyList = distributeWeightCompanyService.listPageByQuery(query);
        // 公司查询条件 - 可能会复制 companyId 和 plat
        BaseCompanyQuery companyQuery = BeanMapper.map(query, BaseCompanyQuery.class);
        //List<BaseCompany> companyList = baseCompanyService.listByQuery(companyQuery);
        List<BaseCompany> companyList = baseCompanyService.listByPlatAndCityIdAndCompanyId(query.getPlat(),
                                                                                         query.getCityId(),
                                                           query.getCompanyId());
        Map<Integer, String> companyMap = Optional.ofNullable(companyList).orElse(Collections.emptyList()).stream()
                .collect(Collectors.toMap(BaseCompany::getCompanyId, BaseCompany::getName));
        Map<Integer, String> cityMap = baseAreaService.listAllCity().stream().collect(Collectors.toMap(DictModel::getKey, DictModel::getValue));
        List<DistributeWeightCompanyVO> voList = distributeWeightCompanyList.stream().map(entityToVoFunction(companyMap, cityMap)).collect(Collectors.toList());
        // log.info("#派单参数配置#服务商配置#列表查询 ： voList = {}", voList);
        List<Integer> distributeConfigIdList = voList.stream().map(DistributeWeightCompanyVO::getWeightId).collect(Collectors.toList());
        DistributeWeightRelationQuery relationQuery = DistributeWeightRelationQuery.builder()
                .distributeConfigIdList(distributeConfigIdList)
                .distributeApplication(DistributeWeightRelationConstant.DISTRIBUTE_APPLICATION_COMPANY)
                .build();
        List<DistributeWeightRelation> distributeWeightRelationList = CollectionUtil.isNullOrEmpty(distributeConfigIdList) ?
                new ArrayList<>() :
                distributeWeightRelationService.listByQuery(relationQuery);
        Map<Integer, List<DistributeWeightRelation>> distributeWeightRelationMap = distributeWeightRelationList.stream().collect(Collectors.groupingBy(DistributeWeightRelation::getDistributeConfigId));
        voList.forEach(d -> d.setDistributeWeightRelationList(distributeWeightRelationMap.get(d.getWeightId())));
        //获取抢单配置关联数据
        List<Integer> robOrderConfigIdList = distributeWeightCompanyList.stream().map(DistributeWeightCompany::getConfigId).collect(Collectors.toList());
        RobOrderRelationQuery robOrderRelationQuery = RobOrderRelationQuery.builder()
                .robOrderConfigIdList(robOrderConfigIdList)
                .build();
        List<RobOrderRelation> robOrderRelationList = CollectionUtil.isNullOrEmpty(robOrderConfigIdList) ?
                new ArrayList<>() :
                robOrderConfigBService.listByQuery(robOrderRelationQuery);
        Map<Integer, List<RobOrderRelation>> robOrderRelationMap = robOrderRelationList.stream().collect(Collectors.groupingBy(RobOrderRelation::getConfigId));
        voList.forEach(d -> d.setRobOrderRelationList(robOrderRelationMap.get(d.getConfigId())));
        mav.addObject("items", voList);
        mav.addObject("vo", query);
        mav.addObject("companyList", listCompanyByPlat(query.getPlat()));
        mav.addObject("cityList", super.loadAllCityData());
        mav.addObject("platList", GlobalConsts.getPlatList());
        mav.addObject("autoStatusSet", EnumSet.allOf(OrderConfigConsts.AutoStatusEnum.class));
        mav.addObject("reworkStatusSet", EnumSet.allOf(OrderConfigConsts.ReworkStatusEnum.class));
        mav.addObject("normalStatusSet", EnumSet.allOf(OrderConfigConsts.StatusEnum.class));
        return mav;
    }

    /**
     * 派单参数公司配置新增修改页面跳转
     *
     * @return 公司配置新增修改页面
     */
    @RequestMapping(value = "company/edit")
    @ReadOnlyConnection
    public ModelAndView companyEdit(Integer weightId,Integer configId) {

        DistributeWeightCompany configCompany = distributeWeightCompanyService.findByKey(weightId);
        DistributeWeightCompanyVO vo = BeanMapper.map(configCompany, DistributeWeightCompanyVO.class);
        ModelAndView mav = Objects.equals(configCompany.getBizType(), ProductConsts.BIZ_TYPE_C) ?
                new ModelAndView("orderconfig/distribute/company/edit") :
                new ModelAndView("orderconfig/distribute/biztypef/company/edit");
        DistributeWeightRelationQuery query = DistributeWeightRelationQuery.builder()
                .distributeConfigId(weightId)
                .distributeApplication(DistributeWeightRelationConstant.DISTRIBUTE_APPLICATION_COMPANY)
                .build();
        List<DistributeWeightRelation> distributeWeightRelationList = NumberUtil.isNullOrZero(weightId) ? new ArrayList<>() :
                distributeWeightRelationService.listByQuery(query);
        vo.setDistributeWeightRelationList(distributeWeightRelationDefaultData(distributeWeightRelationList, DistributeWeightRelationConstant.DISTRIBUTE_APPLICATION_COMPANY, weightId));
        // 修改抢单配置相关信息
        RobOrderConfigInfo robOrderConfigInfo = robOrderConfigBService.findByKey(configId);
        RobOrderRelationQuery robOrderRelationQuery = RobOrderRelationQuery.builder()
                .configId(robOrderConfigInfo.getConfigId())
                .build();
        List<RobOrderRelation> robOrderRelationList = NumberUtil.isNullOrZero(configId)
                ?Collections.emptyList()
                :robOrderConfigBService.listByQuery(robOrderRelationQuery);
        robOrderConfigInfo.setRobOrderRelationList(robOrderRelationList);
        //获取抢单配置关联产品数据

        if(CollectionUtil.isNotNullOrEmpty(robOrderConfigInfo.getRobOrderRelationList())){
            robOrderConfigInfo.getRobOrderRelationList().forEach(p->{
                RobOrderRelationProductQuery robOrderRelationProductQuery =RobOrderRelationProductQuery.builder()
                        .relationId(p.getRelationId())
                        .build();
                List<RobOrderRelationProduct> robOrderRelationProductList = robOrderConfigBService.listProductByQuery(robOrderRelationProductQuery);
                p.setRobOrderRelationProductList(robOrderRelationProductList);
            });
        }
        mav.addObject("obj", vo);
        mav.addObject("robOrderObj", robOrderConfigInfo);
        mav.addObject("engineerList", DistributeWayEnum.getEngineerList());
        mav.addObject("platList", GlobalConsts.getPlatList());
        mav.addObject("companyList", listCompanyByPlat(configCompany.getPlat()));
        mav.addObject("cityList", super.loadAllCityData());
        return mav;
    }

    @RequestMapping(value = "company/add")
    @ReadOnlyConnection
    public ModelAndView companyAdd(Integer plat, Integer bizType) {

        ModelAndView mav = Objects.equals(bizType, ProductConsts.BIZ_TYPE_C) ?
                new ModelAndView("orderconfig/distribute/company/edit") :
                new ModelAndView("orderconfig/distribute/biztypef/company/edit");
        DistributeWeightCompanyVO vo = new DistributeWeightCompanyVO();
        vo.setPlat(plat);
        vo.setBizType(bizType);
        List<DistributeWeightRelation> distributeWeightRelationList = new ArrayList<>();
        vo.setDistributeWeightRelationList(distributeWeightRelationDefaultData(distributeWeightRelationList, DistributeWeightRelationConstant.DISTRIBUTE_APPLICATION_COMPANY, null));
        //新增抢单配置相关信息
        RobOrderConfigInfo robOrderConfigInfo = RobOrderConfigInfo.builder().bizType(bizType).plat(GlobalConsts.PLAT_MARK_ZMN).build();
        List<RobOrderRelation> robOrderRelationList = Collections.emptyList();
        robOrderConfigInfo.setRobOrderRelationList(robOrderRelationList);


        mav.addObject("obj", vo);
        mav.addObject("robOrderObj", robOrderConfigInfo);
        mav.addObject("engineerList", DistributeWayEnum.getEngineerList());
        mav.addObject("platList", GlobalConsts.getPlatList());
        mav.addObject("companyList", listCompanyByPlat(plat));
        mav.addObject("cityList", super.loadAllCityData());
        return mav;
    }

    @RequestMapping(value = "listCompanyCity")
    public void listCompanyCity(HttpServletResponse response, @RequestParam(value = "parentId", defaultValue = "1") int companyId) {
        if (companyId == 0) {
            ResponseUtil.outWrite(response, "[]");
            return;
        }

        List<VtDTO> vtList = baseCompanyAreaService.listVtByManageCompanyId(companyId);
        StringBuilder sb = new StringBuilder("[");
        String jsonFormat = "[\"%s\",\"%s\"]";
        String formattedJson =
                vtList.stream().map(item -> String.format(jsonFormat, item.getValue(), item.getText())).collect(Collectors.joining(","));
        sb.append(formattedJson);
        sb.append("]");

        ResponseUtil.outWrite(response, sb.toString());
    }

    @NotNull
    private Function<DistributeWeightCompany, DistributeWeightCompanyVO> entityToVoFunction(Map<Integer, String> companyMap,
                                                                                            Map<Integer, String> cityMap) {
        return item -> {
            DistributeWeightCompanyVO vo = BeanMapper.map(item, DistributeWeightCompanyVO.class);
            if (Objects.nonNull(vo.getCompanyId())) {
                vo.setCompanyName(Optional.ofNullable(companyMap.get(item.getCompanyId())).orElse("未知公司"));
            }
            if (Objects.nonNull(vo.getCityId())) {
                vo.setCityName(Optional.ofNullable(cityMap.get(item.getCityId())).orElse("未知城市"));
            }
            return vo;
        };
    }

    /**
     * 根据公司ID加载城市信息
     *
     * @param companyId 公司ID
     */
    @ReadOnlyConnection
    @RequestMapping("/loadCityByCompanyId")
    public void loadCityByCompanyId(HttpServletRequest request, HttpServletResponse response, Integer companyId, Integer bizType) {

        List<VtDTO> cityList = Lists.newArrayList();
        if (Objects.nonNull(companyId) && Objects.nonNull(bizType)) {
            cityList = distributeWeightCompanyService.listCityByCompanyId(companyId, bizType);
        }

        StringBuilder sb = new StringBuilder("[");
        String jsonFormat = "[\"%s\",\"%s\"]%s ";

        for (int i = 0; i < cityList.size(); i++) {
            VtDTO item = cityList.get(i);
            String name = item.getText();
            sb.append(String.format(jsonFormat, cityList.get(i).getValue(), name, (i == cityList.size() - 1 ? "" : ",")));
        }
        sb.append("]");

        ResponseUtil.outWrite(response, sb.toString());

    }
}
